<?php
/***
 * ActionLeaf (www.actionleaf.com / actionleaf.googlecode.com) PHP Framework
 * Copyright (c) 2008, Wess D. Cope <wess@wattz.net>
 * All rights reserved.								
 * 													
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:                   
 * 																				
 * 	* Redistributions of source code must retain the above copyright notice,	
 * 	this list of conditions and the following disclaimer.						
 * 																				
 * 	* Redistributions in binary form must reproduce the above copyright notice, 
 * 	this list of conditions and the following disclaimer in the documentation   
 * 	and/or other materials provided with the distribution. 						
 * 																				
 * 	* Neither the name of the <ORGANIZATION> nor the names of its contributors may
 * 	be used to endorse or promote products derived from this software without     
 * 	specific prior written permission.											
 * 																				
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 	 
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES  
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 	 
 * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 		 
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED  
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 	 
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 	 
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 	 
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 	 
 * DAMAGE.																				 
 **/

/**
 * Foundation.ALViewProvider
 * Setup and handle templating for page views.  
 * Reads template file and parses and presents using
 * native php as templating language. 
 */

class ALViewProvider
{
	protected $tplDir; // Template Directory
	protected $layout; // Layout (header/footer) for templates
	
	protected $pageVars = array(); //values for variables inside of templates
	protected $post = array();
	
	private $cycleVar;
	
	/**
	 * __construct function to setup template extension and
	 * directory and load into scope of the page class.	
	 *
	 * @param String $templateDirectory
	 */
	public function __construct($templateDirectory = '')
	{
		if(empty($templateDirectory))
			throw new exception("You must specify a templates directory");
		else
			$this->tplDir = $templateDirectory;
			
			
	}
	
	/**
	 * magic get method for returning values set
	 * to the template pageVar variable.
	 *
	 * @param String $var
	 * @return String
	 */
	public function __get($var)
	{
		return $this->pageVars[$var];			
	}
	
	/**
	 * magic set method to set template variable
	 * values.
	 *
	 * @param String $var
	 * @param Any $val
	 */
	public function __set($var, $val)
	{	
		$this->pageVars[$var] = $val;
	}
	
	/**
	 * method for setting the template to use when
	 * the page is rendered
	 *
	 * @param String $template
	 */
	public function setTemplate($template = '')
	{
		if(!empty($template))
			$this->template = $template;
	}
	
	/**
	 * Allows the template to extend a layout placing the
	 * rendered template inside of the layout that is being
	 * extended
	 *
	 * @param String $layout
	 */
	public function extend($layout = '')
	{
		$this->layout = $layout;
	}
	
	/**
	 * render partial template inside of a template (recursive).
	 * Assumes same top domain as templates.  Use: 'blocks/template.html'
	 * for sub directories
	 *
	 * @param string $blockname
	 * @param array $blockvars
	 */
	public function block($blockname = '', array $blockvars = null)
	{
		if(!file_exists($this->tplDir . $blockname))
			return 'Could not locate block';
		
			if($blockvars != null)
				extract($blockvars);
			
		ob_start();
			include($this->tplDir . $blockname);
		return ob_get_clean();
			
	}
	
	public function jsCompress($file)
	{
		list($file, $ext) = explode(".", $file);
		
		if(file_exists($file . "-compressed." . $ext))
			return $file . "-compressed." . $ext;
			
		require_once(ROOT_DIR . 'system/lib/class.JavaScriptPacker.php');

		$packer = new JavaScriptPacker(file_get_contents($file), 'Normal', true, false);
		return file_put_contents($file . "-compressed." . $ext, $packer->pack());
	}
	
	/**
	 * Allows for the switching of 2 values.  Takes 2
	 * values that will be used to switch between when method is called
	 * 
	 * @param var $one
	 * @param var $two
	 */
	public function cycle($one, $two)
	{
		$this->cycleVar = $this->cycleVar == $one? $two : $one;
		return $this->cycleVar;
	}
	
	/**
	 * Used to format a date passed through the template and
	 * returns the newly formatted date.
	 *
	 * @param $date
	 * @param $format
	 */
	public function dateFormat($date, $format=NO)
	{
		$settings = ALFormatParser::yaml(ROOT_DIR . '/system/conf/setup.conf');
		date_default_timezone_set($settings['timeZone']);
		
		$format = $format? $format : "m/d/Y";
		$date = @mktime(strftime($date));
		return date($format, $date);
	}
	
	/**
	 * method for rendering a template to the browser.  Takes
	 * local page vars and and places them in the template replacing
	 * the $var inside the template with the proper value. Then
	 * returning the page output.
	 *
	 * @param String $template
	 * @param Array $pagevars
	 * @return String
	 */
	public function render($template = '', $pagevars = array())
	{
		if(!empty($pagevars))
			extract($pagevars);
		
		if(!empty($this->pageVars))
			extract($this->pageVars);
		
		ob_start();
			require($this->tplDir . $template);
		$pageContent = ob_get_clean();
		
		if(isset($this->layout) && !empty($this->layout))
		{
			$$template = $pageContent;
			ob_start();
				require($this->tplDir . $this->layout);
			$page = ob_get_clean();
		}
		else
		{
			$page = $pageContent;
		}
		
		return $page;
	}
	
}
